package com.wmx.thread.synchronize_and_lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**********************************
 * @author wmx
 * @date 2019/8/21 15:54
 * @description:
 * @version 1.0
 ************************************/
public class LockTryTest {
    private static ReentrantLock reentrantLock = new ReentrantLock();

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                insert();
            }).start();
        }

    }

    public static void insert() {
        try {
            Condition condition = reentrantLock.newCondition();
            reentrantLock.lock();
            System.out.println(Thread.currentThread().getName() + "获得锁成功================");
            condition.await();
            Thread.sleep(100);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            reentrantLock.unlock();
            System.out.println(Thread.currentThread().getName() + "释放锁");
        }


    }
}
